<template>
  <div>
    <el-dialog
      :title="deviceInfo.id ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="500px"
      :before-close="closeEditDialog"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-form :model="form" :rules="rules" ref="refForm" label-width="80px">
        <el-form-item size="small" label="产品名称" prop="productKey">
          <el-select
            v-model="form.productKey"
            placeholder="请选择产品名称"
            style="width: 380px"
          >
            <el-option
              v-for="item in productList"
              :key="item.id"
              :label="item.name"
              :value="item.productKey"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item size="small" label="设备名称" prop="deviceName">
          <el-input
            clearable
            v-model.trim="form.deviceName"
            placeholder="请输入设备名称"
            maxlength="20"
          ></el-input>
        </el-form-item>
        <el-form-item size="small" label="设备编码" prop="imei">
          <el-input
            clearable
            v-model.trim="form.imei"
            placeholder="请输入设备编码"
            maxlength="15"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeEditDialog" class="cancel-button">取 消</el-button>
        <el-button
          size="small"
          @click="editSubmit"
          v-preventReClick="1000" class="confirm-button"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { reqDeviceAdd, reqDeviceSave, reqProductList } from "@/api/model";

export default {
  props: ["deviceInfo"],
  data() {
    return {
      dialogVisible: false,
      form: {
        productKey: "",
        deviceName: "",
        imei: "",
      },

      // 表单验证规则
      rules: {
        productKey: [
          { required: true, message: "请选择产品名称", trigger: "change" },
        ],
        deviceName: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        ],
        imei: [{ required: true, message: "请输入设备编码", trigger: "blur" }],
      },
      productList: [],
    };
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.getProductList();
      }
    },
    deviceInfo(val) {
      if (!val) return;
      this.form = val;
    },
  },
  methods: {
    // 获取产品列表
    async getProductList() {
      let res = await reqProductList({
        pageNum: 1,
        pageSize: 20,
      });
      if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
      let { rows } = res.data;
      this.productList = rows;
    },
    // 点击
    async editSubmit() {
      this.$refs.refForm.validate(async (valid) => {
        if (valid) {
          let params = this.form;
          let res;
          if (!params.id) {
            // 新增
            res = await reqDeviceAdd({
              data: params,
            });
          } else {
            // 编辑
            delete params.createAt;
            res = await reqDeviceSave({
              data: params,
            });
          }
          if (res.code !== 20000) return this.$modal.notifyWarning(res.message);
          this.$modal.notifySuccess("操作成功");
          this.$parent.getDataList();
          this.closeEditDialog();
        }
      });
    },
    // 点击关闭弹框
    closeEditDialog() {
      this.$refs.refForm.resetFields();
      this.dialogVisible = false;
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../../../common.less");
</style>
